<?php
declare (strict_types=1);

namespace addons\BuilderPlus\components;

class Button
{
    protected $config = [
        'title'          => '',
        'layerTitle'     => '',
        'icon'           => '',
        'class'          => '',
        'event'          => '',
        'width'          => '60%',
        'height'         => '100%',
        'offset'         => '',
        'allLine'        => false,
        'placeholder'    => '',
        'formType'       => 1,
        'customEvent'    => '',
        'preload'        => false,
        'url'            => '',
        'uploadUrl'      => '',
        'urlParam'       => '',
        'exportType'     => '',
        'exportTitle'    => '',
        'exportUrl'      => '',
        'scriptletOpen'  => '',
        'scriptletClose' => '',
        'options'        => 'lay-options'
    ];

    public function __construct(string $name)
    {
        $this->config = $this->getDefaultButton($name);
        $this->config['uploadUrl'] = url('uploads/file')->build();
    }

    public function title(string $title)
    {
        $this->config['title'] = $title;
        return $this;
    }

    public function layerTitle(string $layerTitle)
    {
        $this->config['layerTitle'] = $layerTitle;
        return $this;
    }

    public function icon(string $icon)
    {
        $this->config['icon'] = $icon;
        return $this;
    }

    public function class(string $class)
    {
        $this->config['class'] = $class;
        return $this;
    }

    public function width(string $width)
    {
        $this->config['width'] = $width;
        return $this;
    }

    public function height(string $height)
    {
        $this->config['height'] = $height;
        return $this;
    }

    public function offset(string $offset)
    {
        $this->config['offset'] = $offset;
        return $this;
    }

    public function allLine(bool $allLine)
    {
        $this->config['allLine'] = $allLine;
        return $this;
    }

    public function placeholder(string $placeholder)
    {
        $this->config['placeholder'] = $placeholder;
        return $this;
    }

    public function formType(int $formType)
    {
        $this->config['formType'] = $formType;
        return $this;
    }

    public function event(string $event)
    {
        $this->config['event'] = $event;
        return $this;
    }

    public function customPreload()
    {
        $this->config['options'] = 'preload-options';

    }

    public function customEvent(string $customEvent)
    {
        $this->config['event'] = 'customEvent';
        $this->config['customEvent'] = $customEvent;

        return $this;
    }

    public function url(string $url, string $param = '')
    {
        $this->config['url'] = $url;
        $this->config['urlParam'] = $param;
        return $this;
    }

    public function uploadUrl(string $url)
    {
        $this->config['uploadUrl'] = $url;
        return $this;
    }

    public function scriptlet(string $open, string $close)
    {
        $this->config['scriptletOpen'] = $open;
        $this->config['scriptletClose'] = $close;
        return $this;
    }

    public function render()
    {
        return $this->config;
    }

    public function __call($name, $arguments)
    {
        if (isset($this->config[$name])) {
            $this->config[$name] = $arguments;
        }
        return $this;
    }

    public function getDefaultButton(string $name)
    {
        $defaults = [
            'add'         => [
                'title' => '新增',
                'icon'  => 'layui-icon-add-circle-fine',
                'class' => 'normal',
                'event' => 'add',
                'url'   => url('add')->build(),
            ],
            'edit'        => [
                'title' => '编辑',
                'class' => 'normal',
                'event' => 'edit',
                'url'   => url('edit')->build(),
            ],
            'delete'      => [
                'title' => '删除',
                'class' => 'danger',
                'event' => 'delete',
                'url'   => url('delete')->build(),
            ],
            'deleteAll'   => [
                'title' => '批量删除',
                'icon'  => 'layui-icon-delete',
                'class' => 'danger',
                'event' => 'deleteAll',
                'url'   => url('delete')->build(),
            ],
            'batch'       => [
                'title' => '批量审核',
                'icon'  => 'layui-icon-util',
                'class' => 'danger',
                'event' => 'batch',
                'url'   => url('batch')->build(),
            ],
            'tableExport' => [
                'title' => '导出',
                'icon'  => 'layui-icon-export',
                'class' => 'cyan',
                'event' => 'tableExport',
                'url'   => '',
            ],
            'expandAll'   => [
                'title' => '展开全部',
                'icon'  => 'layui-icon-folder',
                'class' => 'cyan',
                'event' => 'expandAll'
            ]
        ];

        if (isset($defaults[$name])) {
            foreach ($defaults[$name] as $key => $defaultValue) {
                $this->config[$key] = $this->config[$key] ?: $defaultValue;
            }
        } else {
            $this->config['title'] = $this->config['title'] ?: '自定义按钮';
        }

        $this->config['name'] = $name;
        return $this->config;
    }
}
